﻿// (c) Copyright Microsoft Corporation.
// This source is subject to [###LICENSE_NAME###].
// Please see [###LICENSE_LINK###] for details.
// All other rights reserved.

using System;
using System.Globalization;

namespace EnsureWeb.Controls
{
    /// <summary>
    /// The day view of time view scale.
    /// </summary>
    public class DayView : TimeView
    {
        /// <summary>
        /// Initializes a new instances of the DayView class.
        /// </summary>
        /// <param name="timeline">The timeline control.</param>
        public DayView(MultiScaleTimeline timeline)
            : base(timeline, TimelineScale.Day)
        {
        }

        /// <summary>
        /// Gets the total numbers of current items.
        /// </summary>
        protected override int ItemsCount
        {
            get
            {
                int year = this.BeginDate.Year;
                int month = this.BeginDate.Month;
                return DateTime.DaysInMonth(year, month);
            }
        }

        /// <summary>
        /// Gets the next date which is selectable.
        /// </summary>
        public override DateTime NextSelectableDate
        {
            get
            {
                return this.SelectedDate.AddDays(1);
            }
        }

        /// <summary>
        /// Gets the rollup the text.
        /// </summary>
        public override string RollupText
        {
            get
            {
                return string.Format(CultureInfo.InvariantCulture, "{0}, {1}", BeginDate.ToString("MMM", DateTimeHelper.GetCurrentDateFormat()), this.BeginDate.Year);
            }
        }

        /// <summary>
        /// Gets the rollup date.
        /// </summary>
        protected override DateTime RollupDate
        {
            get
            {
                return new DateTime(this.CurrentDate.Year, 1, 1);
            }
        }

        /// <summary>
        /// Gets the start of display date range.
        /// </summary>
        public override DateTime BeginDate
        {
            get
            {
                return new DateTime(CurrentDate.Year, CurrentDate.Month, 1);
            }
        }

        /// <summary>
        /// Gets the end of display date range.
        /// </summary>
        public override DateTime EndDate
        {
            get
            {
                return new DateTime(CurrentDate.Year, CurrentDate.Month, DateTime.DaysInMonth(CurrentDate.Year, CurrentDate.Month));
            }
        }

        /// <summary>
        /// Move forward the display date, used when animation out of display.
        /// </summary>
        public override void NextPage()
        {
            UpdateDate(this.BeginDate.AddMonths(1));
        }

        /// <summary>
        /// Move backward the display date, used when animation out of display.
        /// </summary>
        public override void PreviousPage()
        {
            UpdateDate(this.BeginDate.AddMonths(-1));
        }

        /// <summary>
        /// Gets the timeline item text by index.
        /// </summary>
        /// <param name="index">Item index.</param>
        /// <returns>The timeline item text.</returns>
        protected override string GetTimelineItemText(int index)
        {
            int day = this.BeginDate.Day + index;
            return day.ToString(CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// Check whether the datetime is selected.
        /// </summary>
        /// <param name="dateTime">The datetime checked.</param>
        /// <returns>Whether the datetime is selected.</returns>
        public override bool IsDateSelected(DateTime dateTime)
        {
            return (dateTime <= this.SelectedDate && dateTime > this.SelectedDate.AddDays(-1));
        }

        /// <summary>
        /// Get the date of timeline item.
        /// </summary>
        /// <param name="index">Index of the item.</param>
        /// <returns>Date of timeline item.</returns>
        protected override DateTime GetTimelineItemDate(int index)
        {
            DateTime itemDate = this.BeginDate.AddDays(index);

            return itemDate;
        }

        /// <summary>
        /// Check whether the item is selectable.
        /// </summary>
        /// <param name="index">The index of the item.</param>
        /// <returns>If the item is selectable.</returns>
        public override bool IsItemSelectable(int index)
        {
            DateTime dateStart = this.GetTimelineItemDate(index);
            DateTime dateEnd = new DateTime(dateStart.Year, dateStart.Month, dateStart.Day, 23, 59, 59);

            bool canSel = IsDateRangeSelectable(dateStart, dateEnd);

            return canSel;
        }
    }
}
